<?php 

namespace Home\Controller;
use Think\Controller;
use Home\Controller\CommonController;
class QuestionController extends CommonController
{

	public function howtoas()
    {
    	$this->display();
    }

    public function ask()
    {
    	layout(false);
    	$this->display();
    }
    // 提问题
    public function add()
    {
    	$data['content'] = I('post.editorValue','');
    	$data['title'] = I('post.title','');
    	$data['uid'] = $_SESSION['id'];
    	$data['create_time']=time();

    	$insertId = M('question')->add($data);


    	$cids = I('post.cid','');

    	foreach ($cids as $row) {
    		$insertData['qid'] = $insertId;
    		$insertData['cid'] = $row;
    		M('question_category')->add($insertData);
    	}

    	$this->success('发布成功',U('home/question/ans',array('qid'=>$insertId)));
    }

    public function ueditor()
    {
        $data = new \Org\Util\Ueditor();
        echo $data->output();
    }

    // 展示问题，提问者，相关的类型
    public function ans()
    {
        $qid  = I('get.qid');// 获取问题ID
        $sql  = "select * from question where id='{$qid}'";// 获取那些人提了那些问题
        $data = M()->query($sql);

        $sql  = "select * from answer where qid='{$qid}'";// 获取那些问题那个人回答了
        $answers = M()->query($sql);
        $this->assign('data',current($data));
        $this->assign('answers',$answers);
        // 获取该问题的分类
        $sql = "select t3.cname from question as t1 inner join question_category as t2 on t1.id=t2.qid inner join category as t3 on t2.cid=t3.id where t1.id=$qid";
        $fenlei = M()->query($sql);
        $this->assign('fenlei',$fenlei);        
        // 获取用户信息，那个用户提问了那个问题
        $sql  = "select t2.username from question t1 inner join user t2 on t1.uid=t2.id where t1.id='{$qid}'";
        $yonghu = M()->query($sql);
        $this->assign('yonghu',$yonghu);
        // 那个用户回答那个问题
        $sql = "select t2.username,t2.face from answer t1 inner join user t2 on t1.uid=t2.id where t1.qid='{$qid}' ";
        $answerdata = M()->query($sql);
        // 数组合并，具体哪个人回答了那个具体的问题     
        $newarr = array();
        foreach ($answerdata as $k => $v) {
            $newarr[$k] = array_merge($answerdata[$k], $answers[$k]);
        }
        $this->assign('newarr',$newarr);
        $this->display();
    }
    // 回复问题
    public function add1()
    {
        $data['content'] = I('post.content','');
        $data['qid'] = I('post.qid','');
        $data['uid'] = $_SESSION['id'];
        $data['create_time'] = time();
        M('answer')->add($data);

        $sql = "select answer_num from question where id = '{$data['qid']}' ";
        $res = M()->query($sql);
        $res = $res[0]['answer_num'] +1;

        $sql = "update question set answer_num='$res' where id = '{$data['qid']}' ";
        $res = M()->execute ($sql);

        $this->success('回复成功',U('home/question/ans',array('qid'=>$data['qid'])));
    }


}









 ?>